package net.caixiaomi.info.adapter;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseMultiItemQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.ArticleItem;
import net.caixiaomi.info.util.Constants;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ArticleAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    private boolean mEdit = false;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ArticleAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(Constants.ITEM_NEWS_NO_IMAGE, R.layout.item_news_no_image);
        addItemType(Constants.ITEM_NEWS_ONE_IMAGE, R.layout.item_news_one_image);
        addItemType(Constants.ITEM_NEWS_THREE_IMAGE, R.layout.item_news_three_image);
        addItemType(Constants.ITEM_NEWS_VEDIO, R.layout.item_news_one_image);
    }

    public void setEdit(boolean edit) {
        mEdit = edit;
        notifyDataSetChanged();
    }

    public boolean isEdit() {
        return mEdit;
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        try {
            switch (item.getItemType()) {
                case Constants.ITEM_NEWS_NO_IMAGE:
                    bindNoImage(helper, item);
                    break;
                case Constants.ITEM_NEWS_ONE_IMAGE:
                    bindOneImage(helper, item);
                    break;
                case Constants.ITEM_NEWS_THREE_IMAGE:
                    bindThreeImage(helper, item);
                    break;
                case Constants.ITEM_NEWS_VEDIO:
                    bindVedio(helper, item);
                    break;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void bindVedio(BaseViewHolder helper, MultiItemEntity item) {
        try {
            bindOneImage(helper, item);
            helper.setGone(R.id.flag, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindThreeImage(BaseViewHolder helper, MultiItemEntity item) {
        try {
            int imageWidth = (int) ((AppHelper.getScreenWidth(CommonApp.getInstance()) - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_12) * 2 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_2) * 2) / 3);
            if (mEdit) {
                imageWidth = (int) ((AppHelper.getScreenWidth(CommonApp.getInstance()) - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_12) * 3 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_2) * 2 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_16)) / 3);
            }
            ArticleItem model = (ArticleItem) item;
            helper.setText(R.id.title, model.getTitle());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
            String time = simpleDateFormat.format(new Date(Long.valueOf(model.getAddTime()) * 1000));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(TextUtils.isEmpty(model.getAuthor()) ? "" : model.getAuthor()).append("   ").append(time).append("   ").append(CommonApp.getInstance().getString(R.string.C_READ)).append(!TextUtils.isEmpty(model.getClickNumber()) ? model.getClickNumber() : "");
            helper.setText(R.id.text, stringBuilder);
            ViewGroup.LayoutParams layoutParams = helper.getView(R.id.image1).getLayoutParams();
            layoutParams.width = imageWidth;
            layoutParams.height = imageWidth * 3 / 4;
            layoutParams = helper.getView(R.id.image2).getLayoutParams();
            layoutParams.width = imageWidth;
            layoutParams.height = imageWidth * 3 / 4;
            layoutParams = helper.getView(R.id.image3).getLayoutParams();
            layoutParams.width = imageWidth;
            layoutParams.height = imageWidth * 3 / 4;
            helper.setGone(R.id.btn_delete, mEdit);
            helper.addOnClickListener(R.id.btn_delete);
            int width = layoutParams.width;
            int height = layoutParams.height;
            List<String> articleThumb = model.getArticleThumb();
            if (!articleThumb.isEmpty()) {
                RequestOptions options = new RequestOptions().centerCrop();
                options.override(width, height);
                Glide.with(CommonApp.getInstance()).load(model.getArticleThumb().get(0)).apply(options).into((ImageView) helper.getView(R.id.image1));
                Glide.with(CommonApp.getInstance()).load(model.getArticleThumb().get(1)).apply(options).into((ImageView) helper.getView(R.id.image2));
                Glide.with(CommonApp.getInstance()).load(model.getArticleThumb().get(2)).apply(options).into((ImageView) helper.getView(R.id.image3));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindOneImage(BaseViewHolder helper, MultiItemEntity item) {
        try {
            int imageWidth = (int) ((AppHelper.getScreenWidth(CommonApp.getInstance()) - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_12) * 2 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_2) * 2) / 3);
            if (mEdit) {
                imageWidth = (int) ((AppHelper.getScreenWidth(CommonApp.getInstance()) - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_12) * 3 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_2) * 2 - CommonApp.getInstance().getResources().getDimension(R.dimen.padding_16)) / 3);
            }
            ArticleItem model = (ArticleItem) item;
            helper.setText(R.id.title, model.getTitle());
            ViewGroup.LayoutParams layoutParams = helper.getView(R.id.image).getLayoutParams();
            layoutParams.width = imageWidth;
            layoutParams.height = imageWidth * 3 / 4;
            int width = layoutParams.width;
            int height = layoutParams.height;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
            String time = simpleDateFormat.format(new Date(Long.valueOf(model.getAddTime()) * 1000));

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(model.getAuthor()).append("   ").append(time).append("   ").append(CommonApp.getInstance().getString(R.string.C_READ)).append(!TextUtils.isEmpty(model.getClickNumber()) ? model.getClickNumber() : "");
            helper.setText(R.id.text, stringBuilder);
            List<String> articleThumb = model.getArticleThumb();
            if (!articleThumb.isEmpty()) {
                RequestOptions options = new RequestOptions().centerCrop();
                options.override(width, height);
                Glide.with(CommonApp.getInstance()).load(model.getArticleThumb().get(0)).apply(options).into((ImageView) helper.getView(R.id.image));
            }
            helper.setGone(R.id.btn_delete, mEdit);
            helper.addOnClickListener(R.id.btn_delete);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindNoImage(BaseViewHolder helper, MultiItemEntity item) {
        try {
            ArticleItem model = (ArticleItem) item;
            helper.setText(R.id.title, model.getTitle());
            helper.setText(R.id.content, model.getSummary());

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
            String time = simpleDateFormat.format(new Date(Long.valueOf(model.getAddTime()) * 1000));
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(model.getAuthor()).append("   ").append(time).append("   ").append(CommonApp.getInstance().getString(R.string.C_READ)).append(!TextUtils.isEmpty(model.getClickNumber()) ? model.getClickNumber() : "");
            helper.setText(R.id.text, stringBuilder);
            helper.setGone(R.id.btn_delete, mEdit);
            helper.addOnClickListener(R.id.btn_delete);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
